import request from 'umi-request';

export default {
  namespace: 'employee',

  //用mock的方式
  state: [],

  // //不用mock，state初始值在此文件中的方式
  // state: [
  //   {
  //     id: '001',
  //      name: '张三',
  //   "key": "1",
  //   "firstName": "John",
  //   "lastName": "Brown",
  //   "age": 32,
  //   "address": "New York No. 1 Lake Park",
  //   "tags": ["nice", "developer"],
  //   },
  //   {
  //     id: '002',
  //      name: '李四',
  //   "key": "2",
  //   "firstName": "Jim",
  //   "lastName": "Green",
  //   "age": 42,
  //   "address": "London No. 1 Lake Park",
  //   "tags": ["loser"],
  //   },
  //   {
  //     id: '003',
  //     name: '王五',
  //   "key": "3",
  //   "firstName": "Joe",
  //   "lastName": "Black",
  //   "age": 32,
  //   "address": "Sidney No. 1 Lake Park",
  //   "tags": ["cool", "teacher"],
  //   }
  //  ],

  reducers: {
    //原案例
    // update(_, { payload }) {
    //   const { employees } = payload;
    //   console.log("payloadaaaa",payload);
    //   console.log("employeesaaa",employees);
    //   return employees;
    // },

    //不用mock，state初始值在此文件中的方式,未真实修改mock中的employee
    update(state, { payload }) {
      if(typeof payload == 'undefined'){
        return state;
      }else{
        const { employees } = payload;
        console.log("payloadaaaa",payload);
        console.log("employeesaaa",employees);
        state=employees;
        return employees;
      }
    },

    updateAll(state, { payload }) {
      const {employee} = payload;
      console.log("updateAllInfo-state",state);
      console.log("updateAllInfo-payload",payload);
      console.log("updateAllInfo-employees",employee);
      var allInfo=[];
      employee.map((v, k) => {
        allInfo.push(v);
      });

      state=allInfo
      return allInfo;
    },

    // updateSingle(_, { payload }) {
    //   const singleInfo = payload;
    //   console.log("payload",payload);
    //   return singleInfo;
    // },

    // getId(state, { payload }) {
    //   const { key: targetId } = payload;

    //   return state.key = key;
    // },

    deleteEmployee(state, { payload }) {
      const { key: targetId } = payload;
      console.log("state.filter((e) => e.key !== targetId);",state.filter((e) => e.key !== targetId));
        //采用mock将body内容添加到data.json

      return state.filter((e) => e.key !== targetId);
    },

    addEmployee(state,{payload}){
      // const {key:targetId} = payload;
      // return state.push(payload.map((val,index)=>(
      //   payload[index]===val
      // )))
      const {employee} = payload;
      console.log("addEmployee-state",state);
      console.log("addEmployee-payload",payload);
      return state.concat([employee]);
    }
  },

  effects: {
    //采用mock方式，进行修改及判断是否对employee数据进行了修改
    *getAll(state, action) {
      const employee = state.employee;
      console.log("getAllstate",state);
      const { call, put } = action;
      const response = yield call(request.get, '/api/data');
      //之前看题目，猜想了其他功能，导致的逻辑
      // if(employee == response){
      //   console.log("employee == response");
      //   yield put({
      //     type: 'update',
      //     payload: {
      //       employees: response,
      //     },
      //   });
      // }else{
      //   console.log("employee != response");
      //   yield put({
      //     type: 'update',
      //     payload: {
      //       employees: employee,
      //     },
      //   });
      // }
      yield put({
        type: 'update',
        payload: {
          //employees: employee,
          employees: response,
        },
      });

    },

    //采用mock方式，进行修改及判断是否对employee数据进行了修改
    // *getAllCommit(state, action) {
    //   const employee = state.employee;
    //   console.log("getAllstate",state);
    //   const { call, put } = action
    //   const response = yield call(request.get, '/api/data');//

    //   yield put({
    //     type: 'update',
    //     payload: {
    //       employees: response,
    //     },
    //   });
    // },
    
    *postAll(sData, { call, put }) {
      const {payload:{employee}} = sData;


      console.log("postAll-sData",sData);
      console.log("postAll-employee",employee);

      const response = yield call(request.post, '/api/data',{data:employee});//
      yield put({
        type: 'update',
        payload: {
          employees: response,
        },
      });
    },

    // //原案例，用mock但是数据更改不了，因为一直读的mock里的初始数据
    // *getAll(_, { call, put }) {
    //   const response = yield call(request.get, '/api/employees');//
    //   yield put({
    //     type: 'update',
    //     payload: {
    //       employees: response,
    //     },
    //   });
    // },

  },
};
